package com.youmingxing.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.xiaomi.mipush.sdk.MiPushClient;
import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.data.GlobalDataManager;
import com.youmingxing.network.AccountApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.utils.StringUtil;
import com.youmingxing.view.TextCountWatcher;

import org.json.JSONObject;

/**
 * @author evilyin(ChenZhixi)
 * @since 2015-10-16
 */

public class PasswordActivity extends BaseActivity {

    String number;
    String code;
    int type; // 0 是注册时跳转 1 是修改密码时跳转 2 是绑定手机时跳转
    public static final String EXTRAS_TYPE = "type";
    public static final String EXTRAS_MOBILE = "mobile";
    public static final String EXTRAS_CODE = "code";
    public static final int TYPE_REGISTER = 0;
    public static final int TYPE_MODIFY_PWD = 1;
    public static final int TYPE_BIND = 2;
    public static final int TYPE_FORGET_PWD = 3;
    private Button finish;

    boolean pwdChecked = false;
    boolean confirmChecked = false;
    boolean oldChecked = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_password);
        setTopbarName("设置密码");

        Bundle bundle = getIntent().getExtras();
        number = bundle.getString(EXTRAS_MOBILE);
        code = bundle.getString(EXTRAS_CODE);
        type = bundle.getInt(EXTRAS_TYPE);

        finish = (Button) findViewById(R.id.finish_button);
        final EditText password = (EditText) findViewById(R.id.password);
        final EditText confirm = (EditText) findViewById(R.id.password_confirm);
        final EditText old_password = (EditText) findViewById(R.id.old_password);

        if (type == TYPE_MODIFY_PWD) {
            oldChecked = false;
            old_password.setVisibility(View.VISIBLE);
            old_password.addTextChangedListener(new TextCountWatcher(6) {
                @Override
                public void onTextMatchCount() {
                    oldChecked = true;
                    if (pwdChecked && confirmChecked) {
                        canClick();
                    }
                }

                @Override
                public void onTextLessThanCount() {
                    oldChecked = false;
                    notClick();
                }
            });
        }


        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd = password.getText().toString();
                String pwdConfirm = confirm.getText().toString();
                String pwdOld = old_password.getText().toString();
                if (pwd.length() < 6) {
                    showToast("密码长度太短");
                } else {
                    if (pwd.equals(pwdConfirm)) {
                        showDialog("提交信息中");
                        String md5 = StringUtil.generateMD5(pwd);
                        if (type == TYPE_REGISTER) {
                            AccountApi.register(number, md5, code, jsonHandler);
                        } else if (type == TYPE_MODIFY_PWD) {
                            AccountApi.modifyPassword(number, StringUtil.generateMD5(pwdOld), md5, jsonHandler);
                        } else if (type == TYPE_BIND) {
                            AccountApi.bindOtherAccount("mobile", number, md5, jsonHandler);
                        } else if (type == TYPE_FORGET_PWD) {
                            AccountApi.forgetPassword(number, code, md5, jsonHandler);
                        }
                    } else {
                        showToast("两次输入的密码不符合");
                    }
                }

            }
        });
        notClick();
        password.addTextChangedListener(new TextCountWatcher(6) {
            @Override
            public void onTextMatchCount() {
                pwdChecked = true;
                if (oldChecked && confirmChecked) {
                    canClick();
                }
            }

            @Override
            public void onTextLessThanCount() {
                pwdChecked = false;
                notClick();
            }
        });
        confirm.addTextChangedListener(new TextCountWatcher(6) {
            @Override
            public void onTextMatchCount() {
                confirmChecked = true;
                if (oldChecked && pwdChecked) {
                    canClick();
                }
            }

            @Override
            public void onTextLessThanCount() {
                confirmChecked = false;
                notClick();
            }
        });
    }

    BaseJsonHandler jsonHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            if (response.optInt("status") == 200) {
                if (type == TYPE_REGISTER) {
                    GlobalDataManager.token = response.optJSONObject("data").optString("token");
                    GlobalDataManager.save();
                    Intent intent = new Intent(PasswordActivity.this, CompleteInfoActivity.class);
                    intent.putExtra("mobile", number);
                    startActivity(intent);
                } else if (type == TYPE_MODIFY_PWD) {
                    GlobalDataManager.token = response.optJSONObject("data").optString("token");
                    GlobalDataManager.save();
                    showToast("密码修改成功");
                } else if (type == TYPE_BIND) {
                    showToast("手机号绑定成功");
                    //暂存绑定手机号,用于completeInfoActivity显示
                    GlobalDataManager.bindPhone = number;
                    GlobalDataManager.save();
                } else if (type == TYPE_FORGET_PWD) {
                    GlobalDataManager.token = response.optJSONObject("data").optString("token");
                    GlobalDataManager.save();
                    showToast("密码修改成功");
                }
                //设置推送别名
                MiPushClient.setAlias(getApplicationContext(), GlobalDataManager.token, null);
                finish();
            } else {
                showToast(response.optString("statusText"));
            }
        }
    };

    private void canClick() {
        finish.setBackgroundResource(R.drawable.btn_redbg);
        finish.setClickable(true);
    }

    private void notClick() {
        finish.setBackgroundResource(R.drawable.btn_unable);
        finish.setClickable(false);
    }
}
